import React, { useCallback, useState, useLayoutEffect, useEffect } from 'react';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from '@ant-design/icons';
import { Button, Layout, Menu, message, theme } from 'antd';
import { Outlet, useNavigate } from 'react-router-dom';
import { homeRoutes } from './router';
import tu from './img/1.webp'
import { Select } from 'antd';
// import i18n from './i18n/i18n';

const { Header, Sider, Content } = Layout;

const App: React.FC = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  const navigate = useNavigate()
  const [item, setItem] = useState([
    {
      key: '1',
      icon: <UserOutlined />,
      label: 'nav 1',
    },
  ])

  const index = useCallback((routes) => {
    const items = routes.map((item) => {
      const res: any = {}
      res.key = item.path
      if (item.icon) {
        res.icon = item.icon
      }
      res.label = item.label

      return res
    })
    return items
  }, [])

  useLayoutEffect(() => {
    setItem(index(homeRoutes))
  }, [])

  const handleRouter = useCallback((obj) => {
    navigate(obj.key)
  }, [])

  const handleBackLogin = () => {
    navigate('/login')
    localStorage.removeItem('token')
    localStorage.removeItem('userId')
    localStorage.removeItem('username')
    message.error('退出登录')
  }

  // 中英文切换
  const handleChange = (value: string) => {

  };
  // 日夜间切换

  const handleChanges = (value: string) => {

  };

  return (
    <Layout style={{ height: '100vh' }}>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={['/']}
          items={item}
          onClick={handleRouter}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <div>
              <Button
                type="text"
                icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
                onClick={() => setCollapsed(!collapsed)}
                style={{
                  fontSize: '16px',
                  width: 64,
                  height: 64,
                }}
              />
              <Select
                defaultValue=''
                style={{ width: 120 }}
                onChange={handleChange}
                options={[
                  { value: 'zh', label: '中文' },
                  { value: 'en', label: 'English' },
                ]}
              />
              <Select
                defaultValue=''
                style={{ width: 120 }}
                onChange={handleChanges}
                options={[
                  { value: 'dark', label: '夜间' },
                  { value: 'light', label: '日间' },
                ]}
              />
            </div>

            <span style={{ display: 'flex', justifyContent: 'space-around', alignItems: 'center' }}>
              <img style={{ width: '40px', height: '40px', borderRadius: "50%" }} src={tu} alt="" />
              {
                localStorage.getItem('username')
              }
              <Button onClick={() => handleBackLogin()}>退出登录</Button>
            </span>
          </div>
        </Header>
        <Content
          style={{
            margin: '24px 16px',
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          <Outlet></Outlet>
        </Content>
      </Layout>
    </Layout>
  );
};

export default App;